package com.woniuxy.educationplatform.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.educationplatform.bean.*;
import com.woniuxy.educationplatform.service.IOrderAddressService;
import com.woniuxy.educationplatform.service.IOrderService;
import com.woniuxy.educationplatform.service.IStudentService;

import com.woniuxy.educationplatform.util.PageDto;
import com.woniuxy.educationplatform.util.ResultUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;
import java.util.Map;

import static com.woniuxy.educationplatform.util.ResultUtil.SUCCESS;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Resource
    private IStudentService studentServiceImpl;
    @Resource
    private IOrderAddressService orderAddressService;
    @Resource
    private IOrderService orderService;


    @DeleteMapping("/del/{id}")
    public ResultUtil delById(@PathVariable Integer id){
        ResultUtil resultUtil = new ResultUtil().setCode(ResultUtil.ERROR).setMessage("系统繁忙，请稍后再试");
        if((studentServiceImpl.deleteStudentBean(Long.valueOf(id)))>0){
            resultUtil.setCode(ResultUtil.SUCCESS).setMessage("删除成功");
        }
        return  resultUtil;
    }
    /**
     * 舍弃
     */
   /* @RequestMapping(value="/all",produces = {"application/json;charset=utf-8"})
    public List<StudentBean> testOne(StudentBean studentBean){
        ResultUtil resultUtil = new ResultUtil();
        List<StudentBean> studentBeans = studentServiceImpl.findAllStudentRecording(studentBean);
        return studentBeans;
    }*/




    /**
     * 查询所有的学校
     *
     */

    @RequestMapping(value="/school",produces = {"application/json;charset=utf-8"})
    public List<SchoolBean> selectAllSchool(){
        return studentServiceImpl.selectAllSchool();
    }


    /**
     *
     * @param studentBean 前端的查询对象
     * @param pageNum 页码数
     * @param pageSize 每页大小
     * @param statusId 前端的角色id 由于不能自动封装，手动setStatusName
     * @return 返回结果
     */
    @RequestMapping("/findList")
    public ResultUtil<PageDto> findAllByParams(StudentBean studentBean, int pageNum, int pageSize, Integer statusId){
        studentBean.setStatusBean(new StatusBean().setId(statusId));
        System.out.println(studentBean);
        PageDto data = studentServiceImpl.findAllByParams(studentBean, pageNum, pageSize);
        return new ResultUtil<PageDto>().setCode(ResultUtil.SUCCESS).setData(data).setMessage("请求成功");
    }


    /**
     *
     *学员开课
     * @param studentBean
     * @param pageNum
     * @param pageSize
     * @param schoolId
     * @return
     */
    @RequestMapping("/findClassList")
    public ResultUtil<PageDto> findAllClassByParams(StudentBean studentBean, int pageNum, int pageSize, Integer schoolId){
        if((schoolId != null) && (!"".equals(schoolId) && (!"null".equals(schoolId)))){
          studentBean.setSchool(new SchoolBean().setId(schoolId));
        }
        PageDto data = studentServiceImpl.findAllClassByParams(studentBean, pageNum, pageSize);
        return new ResultUtil<PageDto>().setCode(ResultUtil.SUCCESS).setData(data).setMessage("请求成功");
    }

    @RequestMapping("/findRecordList")
    public ResultUtil<PageDto> findAllRecordByParams(StuVideosBean stuVideosBean, int pageNum, int pageSize, Integer studentId,String videoName){



        if(((studentId != null) && (!"".equals(studentId) && (!"null".equals(studentId))))&&((videoName != null) && (!"".equals(videoName) && (!"null".equals(videoName))))){
            stuVideosBean.setStudent(new StudentBean().setId(studentId)).setVideo(new VideoBean().setVideoName(videoName));
        }
        else if((studentId != null) && (!"".equals(studentId) && (!"null".equals(studentId)))){
            stuVideosBean.setStudent(new StudentBean().setId(studentId));
        }
        else if((videoName != null) && (!"".equals(videoName) && (!"null".equals(videoName)))){
            stuVideosBean.setVideo(new VideoBean().setVideoName(videoName));
        }
        System.out.println("------------------------------------");
        System.out.println(stuVideosBean);
        PageDto data = studentServiceImpl.findAllRecordsByParams(stuVideosBean,pageNum,pageSize);
      //  System.out.println(data);
        return new ResultUtil<PageDto>().setCode(ResultUtil.SUCCESS).setData(data).setMessage("请求成功");
    }

    /**
     * 通过传来的id找到那一个学生的信息
     * @param myId
     * @return
     */
    @RequestMapping("/findOne")
    public ResultUtil<StudentBean> findOneByParams(Integer myId){
        StudentBean data = studentServiceImpl.findStudentById(myId);
      //  System.out.println(data);
        return new ResultUtil<StudentBean>().setCode(ResultUtil.SUCCESS).setData(data).setMessage("请求成功");
    }

    @RequestMapping("/findAllAddress")
    public ResultUtil<List<OrderAddress>> findAllAddressByParams(Integer myId){
        List<OrderAddress> data = orderAddressService.findAddressesByStudentId(Long.valueOf(myId));
        return new ResultUtil<List<OrderAddress>>().setCode(ResultUtil.SUCCESS).setData(data).setMessage("请求成功");
    }

    @RequestMapping("/findOrder")
    public ResultUtil<Map<String,Double>> findOrder(Integer myId){
        Map<String,Double> data = orderService.findOrderDetailsByStudentId(Long.valueOf(myId));
        return new ResultUtil<Map<String,Double>>().setCode(ResultUtil.SUCCESS).setData(data).setMessage("请求成功");
    }

    @RequestMapping("/findTest")
    public ResultUtil<List<StuTestBean>> findTest(Integer myId){
        List<StuTestBean> data = studentServiceImpl.findAllTestByParams(myId);
        return new ResultUtil<List<StuTestBean>>().setCode(ResultUtil.SUCCESS).setData(data).setMessage("请求成功");
    }

    /**
     *修改学生信息
     * @param studentBean
     * @param id
     * @return
     */
    @PutMapping("/updateMsg")
    public ResultUtil updateStudentById(StudentBean studentBean,Integer id,Integer statusId) {
        ResultUtil resultUtil = new ResultUtil();
    //    System.out.println(statusId);
        studentBean.setId(id).setStatusBean(new StatusBean().setId(statusId));
     //   System.out.println(studentBean);
        if(studentServiceImpl.updateStudentBean(studentBean)>0){
            return resultUtil.setCode(ResultUtil.SUCCESS).setMessage("修改成功");
        }else {
            return resultUtil.setCode(ResultUtil.ERROR).setMessage("修改失败");
        }
    }

    /**
     * 删除观看记录
     * @param id
     * @return
     */
    @DeleteMapping("/delStuVideo/{id}")
    public ResultUtil delBySvid(@PathVariable Integer id){
        ResultUtil resultUtil = new ResultUtil().setCode(ResultUtil.ERROR).setMessage("系统繁忙，请稍后再试");
        if((studentServiceImpl.deleteStuVideoBean(Long.valueOf(id)))>0){
            resultUtil.setCode(ResultUtil.SUCCESS).setMessage("删除成功");
        }
        return  resultUtil;
    }


}
